﻿#include "IdaDialogFrame.h"

#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

#include "IdaMainWindow.h"

IdaDialogFrame::IdaDialogFrame(QWidget* parent)
    : QDialog(parent), m_windowFrame(new IdaMainWindow(parent)), m_isNoCloseWindow(false)
{
    initDialog();
}

IdaDialogFrame::~IdaDialogFrame()
{
}

/// @brief 初始化对话框

void IdaDialogFrame::initDialog()
{
    m_windowFrame->setCentralWidget(this);
    m_windowFrame->setWindowModality(Qt::WindowModality::ApplicationModal);
    m_windowFrame->setWindowButtonType(BaseWidget::ETitleBar::Close);
    m_windowFrame->setWindowFlags(m_windowFrame->windowFlags() | Qt::Dialog);
    m_windowFrame->setAttribute(Qt::WA_DeleteOnClose);
    m_windowFrame->setTitleBarHeight(24);

    connect(m_windowFrame, &IdaMainWindow::signal_windowStateChange, this, &IdaDialogFrame::signal_windowStateChange);
    connect(m_windowFrame, &IdaMainWindow::signal_closeButtonClick, this, &IdaDialogFrame::slot_closeButtonClick);
}

void IdaDialogFrame::setWindowTitle(QString text)
{
    m_windowFrame->setWindowTitle(text);
}

/// @brief 设置不关闭窗体。点击关闭按钮后只隐藏窗体，不关闭

void IdaDialogFrame::setNoCloseWindow(bool isNoClose)
{
    m_isNoCloseWindow = isNoClose;
}

void IdaDialogFrame::showWindow()
{
    if (this == nullptr)
    {
        qDebug() << QStringLiteral("窗体已关闭，内存已释放！%1.").arg(this->metaObject()->className());
    }

    auto _screen = qApp->screenAt(this->geometry().center());
    if (_screen)
    {
        auto _windowRect = this->geometry();
        _windowRect.moveCenter(_screen->geometry().center());
        m_windowFrame->move(_windowRect.topLeft());
        m_windowFrame->show();
    }
    else
    {
        qDebug() << QStringLiteral("获取屏幕失败！");
    }
}

/// @brief 隐藏窗体

void IdaDialogFrame::hideWindow()
{
    m_windowFrame->hide();
    if (m_loop.isRunning())
        m_loop.exit(0);
}

/// @brief 阻塞显示窗体

int IdaDialogFrame::execWindow()
{
    showWindow();
    return m_loop.exec();
}

/// @brief 关闭窗体

void IdaDialogFrame::closeWindow(int status)
{
    m_windowFrame->close();
    m_loop.exit(status);
}

void IdaDialogFrame::setWindowModality(Qt::WindowModality windowModality)
{
    m_windowFrame->setWindowModality(windowModality);
}

void IdaDialogFrame::setWindowButtonType(BaseWidget::ETitleBars buttonType)
{
    m_windowFrame->setWindowButtonType(buttonType);
}

void IdaDialogFrame::setWindowFlags(Qt::WindowFlags flags)
{
    m_windowFrame->setWindowFlags(flags);
}

void IdaDialogFrame::setWindowSize(int width, int height)
{
    m_windowFrame->resize(width, height);
}

void IdaDialogFrame::setAttribute(Qt::WidgetAttribute attribute, bool on)
{
    m_windowFrame->setAttribute(attribute, on);
}

void IdaDialogFrame::reject()
{
    closeWindow(QDialog::Rejected);
    QDialog::reject();
}

void IdaDialogFrame::accept()
{
    closeWindow(QDialog::Accepted);
    QDialog::accept();
}

void IdaDialogFrame::slot_closeButtonClick()
{
    qDebug() << QStringLiteral("对话框窗体关闭 ： %1.").arg(this->metaObject()->className());

    bool _isAllowClose = true;
    emit IdaDialogFrame::signal_readyCloseDialog(_isAllowClose);
    if (_isAllowClose)
    {
        if (m_isNoCloseWindow)
        {
            hideWindow();
        }
        else
        {
            closeWindow(QDialog::Rejected);
        }
    }
}
